import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtMultimedia

ApplicationWindow {
    id: root
    width: 1400
    height: 900
    visible: true
    title: "5G网络监控系统"
    
    // 背景色
    color: "#f5f7fa"
    
    // 音频播放器（如果音频文件不存在，程序仍可正常运行）
    SoundEffect {
        id: alarmSound
        source: "qrc:/qt/qml/MonitorApp/assets/alarm.wav"
        
        onStatusChanged: {
            if (status === SoundEffect.Error) {
                console.log("警告: 告警音频文件未找到，将使用静默模式")
            }
        }
    }
    
    // 告警对话框
    AlarmDialog {
        id: alarmDialog
    }
    
    // 连接告警信号
    Connections {
        target: alarmModel
        function onNewAlarmAdded(stationId, message, severity) {
            alarmDialog.showAlarm(stationId, message, severity)
            // 播放告警声音（如果音频文件可用）
            if (severity === "critical" && alarmSound.status === SoundEffect.Ready) {
                alarmSound.play()
            }
        }
    }
    
    // 主布局
    ColumnLayout {
        anchors.fill: parent
        anchors.margins: 20
        spacing: 20
        
        // 顶部标题栏
        Rectangle {
            Layout.fillWidth: true
            Layout.preferredHeight: 80
            color: "#2c3e50"
            radius: 10
            
            RowLayout {
                anchors.fill: parent
                anchors.margins: 20
                
                Text {
                    text: "5G网络监控系统"
                    font.pixelSize: 32
                    font.bold: true
                    color: "white"
                }
                
                Item { Layout.fillWidth: true }
                
                // 系统状态指示器
                Rectangle {
                    width: 120
                    height: 40
                    radius: 20
                    color: {
                        switch(networkMonitor.overallStatus) {
                            case "normal": return "#27ae60"
                            case "warning": return "#f39c12"
                            case "critical": return "#e74c3c"
                            default: return "#95a5a6"
                        }
                    }
                    
                    Text {
                        anchors.centerIn: parent
                        text: {
                            switch(networkMonitor.overallStatus) {
                                case "normal": return "● 正常"
                                case "warning": return "● 警告"
                                case "critical": return "● 严重"
                                default: return "● 未知"
                            }
                        }
                        font.pixelSize: 16
                        font.bold: true
                        color: "white"
                    }
                    
                    // 闪烁动画（仅在异常时）
                    SequentialAnimation on opacity {
                        running: networkMonitor.overallStatus !== "normal"
                        loops: Animation.Infinite
                        NumberAnimation { to: 0.3; duration: 500 }
                        NumberAnimation { to: 1.0; duration: 500 }
                    }
                }
                
                Text {
                    text: Qt.formatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss")
                    font.pixelSize: 16
                    color: "white"
                    
                    Timer {
                        interval: 1000
                        running: true
                        repeat: true
                        onTriggered: parent.text = Qt.formatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss")
                    }
                }
            }
        }
        
        // 指标卡片区域
        RowLayout {
            Layout.fillWidth: true
            spacing: 15
            
            MetricCard {
                Layout.fillWidth: true
                Layout.preferredHeight: 140
                title: "基站总数"
                value: networkMonitor.totalStations
                unit: "个"
                icon: "📡"
                statusColor: "#3498db"
                clickable: true
                
                onClicked: stationListPopup.open()
            }
            
            MetricCard {
                Layout.fillWidth: true
                Layout.preferredHeight: 140
                title: "在线用户"
                value: networkMonitor.onlineUsers
                unit: "人"
                icon: "👥"
                statusColor: "#9b59b6"
            }
            
            MetricCard {
                Layout.fillWidth: true
                Layout.preferredHeight: 140
                title: "平均信号质量"
                value: networkMonitor.avgSignalQuality.toFixed(1)
                unit: "%"
                icon: "📶"
                statusColor: networkMonitor.avgSignalQuality >= 75 ? "#27ae60" : 
                           networkMonitor.avgSignalQuality >= 60 ? "#f39c12" : "#e74c3c"
            }
            
            MetricCard {
                Layout.fillWidth: true
                Layout.preferredHeight: 140
                title: "异常基站"
                value: networkMonitor.abnormalStations
                unit: "个"
                icon: "⚠️"
                statusColor: networkMonitor.abnormalStations === 0 ? "#27ae60" : 
                           networkMonitor.abnormalStations < 3 ? "#f39c12" : "#e74c3c"
                isAlert: networkMonitor.abnormalStations > 0
            }
        }
        
        // 中间内容区域
        RowLayout {
            Layout.fillWidth: true
            Layout.fillHeight: true
            spacing: 20
            
            // 左侧：地图和图表
            ColumnLayout {
                Layout.fillWidth: true
                Layout.fillHeight: true
                Layout.preferredWidth: parent.width * 0.6
                spacing: 20
                
                // 地图视图
                MapView {
                    id: mapView
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    Layout.minimumHeight: 350
                }
                
                // 趋势图表
                TrendChart {
                    Layout.fillWidth: true
                    Layout.preferredHeight: 250
                }
            }
            
            // 右侧：饼图和告警列表
            ColumnLayout {
                Layout.fillWidth: true
                Layout.fillHeight: true
                Layout.preferredWidth: parent.width * 0.4
                spacing: 20
                
                // 饼图
                PieChart {
                    Layout.fillWidth: true
                    Layout.preferredHeight: 300
                }
                
                // 告警列表
                Rectangle {
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    color: "white"
                    radius: 10
                    border.color: "#e0e0e0"
                    border.width: 1
                    
                    MouseArea {
                        anchors.fill: parent
                        acceptedButtons: Qt.LeftButton
                        
                        onDoubleClicked: {
                            // 双击整个告警历史区域：全屏显示
                            alarmHistoryFullScreenPopup.open()
                        }
                    }
                    
                    ColumnLayout {
                        anchors.fill: parent
                        anchors.margins: 15
                        spacing: 10
                        
                        RowLayout {
                            Layout.fillWidth: true
                            
                            Text {
                                text: "告警历史"
                                font.pixelSize: 18
                                font.bold: true
                                color: "#2c3e50"
                            }
                            
                            Item { Layout.fillWidth: true }
                            
                            Button {
                                text: "清空"
                                onClicked: alarmModel.clearAll()
                                background: Rectangle {
                                    color: parent.pressed ? "#c0392b" : parent.hovered ? "#e74c3c" : "#95a5a6"
                                    radius: 5
                                }
                                contentItem: Text {
                                    text: parent.text
                                    color: "white"
                                    horizontalAlignment: Text.AlignHCenter
                                    verticalAlignment: Text.AlignVCenter
                                }
                            }
                        }
                        
                        ListView {
                            Layout.fillWidth: true
                            Layout.fillHeight: true
                            model: alarmModel
                            clip: true
                            spacing: 8
                            
                            delegate: Rectangle {
                                width: ListView.view.width
                                height: 70
                                color: model.acknowledged ? "#ecf0f1" : "#fff3cd"
                                radius: 5
                                border.color: model.severity === "critical" ? "#e74c3c" : "#f39c12"
                                border.width: 2
                                
                                ColumnLayout {
                                    anchors.fill: parent
                                    anchors.margins: 10
                                    spacing: 5
                                    
                                    RowLayout {
                                        Layout.fillWidth: true
                                        
                                        Text {
                                            text: model.stationId
                                            font.pixelSize: 14
                                            font.bold: true
                                            color: model.severity === "critical" ? "#e74c3c" : "#f39c12"
                                        }
                                        
                                        Text {
                                            text: model.timestamp
                                            font.pixelSize: 12
                                            color: "#7f8c8d"
                                        }
                                        
                                        Item { Layout.fillWidth: true }
                                    }
                                    
                                    Text {
                                        Layout.fillWidth: true
                                        text: model.message
                                        font.pixelSize: 13
                                        color: "#2c3e50"
                                        wrapMode: Text.WordWrap
                                    }
                                }
                                
                                MouseArea {
                                    anchors.fill: parent
                                    onClicked: {
                                        // 单击：高亮显示对应基站在地图上
                                        mapView.highlightStation(model.stationId)
                                        
                                        // 确认告警
                                        if (!model.acknowledged) {
                                            alarmModel.acknowledgeAlarm(index)
                                        }
                                    }
                                    
                                    onDoubleClicked: {
                                        // 双击：显示报告对话框（传递完整的告警信息）
                                        var alarmInfo = alarmModel.getAlarmInfo(index)
                                        alarmReportDialog.showReportWithInfo(alarmInfo)
                                    }
                                }
                            }
                            
                            ScrollBar.vertical: ScrollBar {
                                policy: ScrollBar.AsNeeded
                            }
                        }
                    }
                }
            }
        }
    }
    
    // 告警报告对话框
    Popup {
        id: alarmReportDialog
        
        x: (parent.width - width) / 2
        y: (parent.height - height) / 2
        width: 600
        height: 500
        modal: true
        focus: true
        closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
        
        property string currentStationId: ""
        property string currentMessage: ""
        property string currentSeverity: "warning"
        property var currentAlarmInfo: null
        
        function showReport(stationId, message, severity) {
            currentStationId = stationId
            currentMessage = message
            currentSeverity = severity
            currentAlarmInfo = null
            open()
        }
        
        function showReportWithInfo(alarmInfo) {
            currentStationId = alarmInfo.stationId
            currentMessage = alarmInfo.message
            currentSeverity = alarmInfo.severity
            currentAlarmInfo = alarmInfo
            open()
        }
        
        function generateReportContent() {
            // 优先使用保存的告警信息，如果没有则从 BaseStationModel 查询
            var stationInfo = null
            
            if (currentAlarmInfo && currentAlarmInfo.stationName) {
                // 使用保存的告警信息（历史快照）
                stationInfo = currentAlarmInfo
            } else {
                // 从 BaseStationModel 查询当前信息
                for (var i = 0; i < baseStationModel.rowCount(); i++) {
                    var info = baseStationModel.getStationInfo(i)
                    if (info.stationId === currentStationId) {
                        stationInfo = info
                        break
                    }
                }
            }
            
            if (!stationInfo) {
                return "未找到基站信息: " + currentStationId
            }
            
            // 生成地理位置编号
            var stationNumber = currentStationId.replace("BS", "").replace(/^0+/, "")
            var positionNumber = stationNumber + "号位"
            
            // 生成报告内容
            var report = "========== 基站异常处理报告 ==========\n"
            report += "报告生成时间: " + Qt.formatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss") + "\n"
            report += "处理人员: 系统管理员\n\n"
            
            report += "【基站信息】\n"
            report += "基站ID: " + stationInfo.stationId + "\n"
            report += "基站名称: " + stationInfo.stationName + "\n"
            report += "地理位置: " + positionNumber + "\n\n"
            
            report += "【告警信息】\n"
            report += "告警级别: " + (currentSeverity === "critical" ? "严重" : 
                                    currentSeverity === "warning" ? "警告" : "一般") + "\n"
            
            // 获取该基站的告警次数统计
            var stationAlarmCount = 0
            if (typeof alarmModel !== 'undefined') {
                stationAlarmCount = alarmModel.getCountByStation(currentStationId)
            }
            report += "历史告警次数: " + stationAlarmCount + " 次\n"
            
            report += "问题描述: " + currentMessage + "\n\n"
            
            report += "【信号参数】\n"
            report += "信号强度: " + stationInfo.signalStrength.toFixed(1) + "%\n"
            report += "基站状态: " + (stationInfo.status === "normal" ? "正常" :
                                    stationInfo.status === "warning" ? "警告" :
                                    stationInfo.status === "critical" ? "严重" : "离线") + "\n"
            report += "连接用户数: " + stationInfo.connectedUsers + " 人\n\n"
            
            report += "【地理坐标】\n"
            report += "纬度: " + stationInfo.latitude.toFixed(6) + "°\n"
            report += "经度: " + stationInfo.longitude.toFixed(6) + "°\n\n"
            
            report += "【处理建议】\n"
            if (currentSeverity === "critical") {
                report += "- 立即派遣技术人员到现场检查\n"
                report += "- 检查基站电源和网络连接\n"
                report += "- 必要时启用备用基站\n"
            } else if (currentSeverity === "warning") {
                report += "- 监控信号质量变化趋势\n"
                report += "- 检查周边环境是否有干扰源\n"
                report += "- 安排维护人员例行检查\n"
            } else {
                report += "- 检查基站设备状态\n"
                report += "- 确认网络连接正常\n"
                report += "- 联系设备供应商技术支持\n"
            }
            
            report += "\n========== 报告结束 ==========\n"
            
            return report
        }
        
        function exportReport() {
            var reportContent = generateReportContent()
            var timestamp = Qt.formatDateTime(new Date(), "yyyyMMdd_HHmmss")
            var fileName = "alarm_report_" + currentStationId + "_" + timestamp + ".txt"
            
            // 调用 NetworkMonitor 的文件保存方法
            if (networkMonitor.saveReportFile(fileName, reportContent)) {
                console.log("✅ 报告已导出到文件:", fileName)
            } else {
                console.log("❌ 报告导出失败")
            }
        }
        
        background: Rectangle {
            color: "white"
            radius: 10
            border.color: "#e0e0e0"
            border.width: 1
            
            // 阴影效果
            Rectangle {
                anchors.fill: parent
                anchors.margins: -5
                color: "black"
                radius: parent.radius
                opacity: 0.1
                z: -1
            }
        }
        
        Column {
            anchors.fill: parent
            anchors.margins: 20
            spacing: 15
            
            // 标题栏
            Row {
                width: parent.width
                spacing: 10
                
                Rectangle {
                    width: 4
                    height: 30
                    color: currentSeverity === "critical" ? "#e74c3c" : "#f39c12"
                    radius: 2
                }
                
                Column {
                    spacing: 5
                    
                    Text {
                        text: "基站异常处理报告"
                        font.pixelSize: 20
                        font.bold: true
                        color: "#2c3e50"
                    }
                    
                    Text {
                        text: "基站ID: " + currentStationId
                        font.pixelSize: 14
                        color: "#7f8c8d"
                    }
                }
                
                Item { width: parent.width - 200 }
                
                Button {
                    text: "关闭"
                    onClicked: alarmReportDialog.close()
                    background: Rectangle {
                        color: parent.pressed ? "#bdc3c7" : parent.hovered ? "#ecf0f1" : "#f8f9fa"
                        radius: 5
                        border.color: "#bdc3c7"
                        border.width: 1
                    }
                }
            }
            
            // 报告内容
            ScrollView {
                width: parent.width
                height: parent.height - 120
                clip: true
                
                TextArea {
                    id: reportTextArea
                    text: alarmReportDialog.generateReportContent()
                    font.family: "Consolas, Monaco, monospace"
                    font.pixelSize: 12
                    color: "#2c3e50"
                    selectByMouse: true
                    readOnly: true
                    wrapMode: TextArea.Wrap
                    
                    background: Rectangle {
                        color: "#f8f9fa"
                        radius: 5
                        border.color: "#e0e0e0"
                        border.width: 1
                    }
                }
            }
            
            // 按钮栏
            Row {
                anchors.horizontalCenter: parent.horizontalCenter
                spacing: 15
                
                Button {
                    text: "📋 复制报告"
                    onClicked: {
                        reportTextArea.selectAll()
                        reportTextArea.copy()
                        reportTextArea.deselect()
                        console.log("报告已复制到剪贴板")
                    }
                    background: Rectangle {
                        color: parent.pressed ? "#2980b9" : parent.hovered ? "#3498db" : "#5dade2"
                        radius: 5
                    }
                    contentItem: Text {
                        text: parent.text
                        color: "white"
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        font.pixelSize: 14
                    }
                }
                
                Button {
                    text: "💾 输出报告"
                    onClicked: {
                        alarmReportDialog.exportReport()
                    }
                    background: Rectangle {
                        color: parent.pressed ? "#27ae60" : parent.hovered ? "#2ecc71" : "#58d68d"
                        radius: 5
                    }
                    contentItem: Text {
                        text: parent.text
                        color: "white"
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        font.pixelSize: 14
                    }
                }
            }
        }
    }
    
    // 基站列表弹窗
    Popup {
        id: stationListPopup
        x: (parent.width - width) / 2
        y: 80
        width: 800
        height: 600
        modal: true
        focus: true
        closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
        
        background: Rectangle {
            color: "white"
            radius: 15
            border.color: "#3498db"
            border.width: 2
            
            // 渐变背景
            Rectangle {
                anchors.fill: parent
                radius: parent.radius
                gradient: Gradient {
                    GradientStop { position: 0.0; color: "#f0f8ff" }
                    GradientStop { position: 1.0; color: "white" }
                }
            }
        }
        
        Column {
            anchors.fill: parent
            anchors.margins: 20
            spacing: 15
            
            // 标题栏
            RowLayout {
                width: parent.width
                
                Text {
                    text: "📡 全部基站列表"
                    font.pixelSize: 24
                    font.bold: true
                    color: "#2c3e50"
                }
                
                Item {
                    Layout.fillWidth: true
                }
                
                Text {
                    text: "共 " + baseStationModel.rowCount() + " 个基站"
                    font.pixelSize: 14
                    color: "#7f8c8d"
                }
                
                Item {
                    width: 15
                }
                
                Button {
                    width: 80
                    height: 35
                    text: "关闭"
                    
                    background: Rectangle {
                        color: parent.pressed ? "#7f8c8d" : parent.hovered ? "#95a5a6" : "#bdc3c7"
                        radius: 8
                    }
                    
                    contentItem: Text {
                        text: parent.text
                        color: "white"
                        font.pixelSize: 14
                        font.bold: true
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                    }
                    
                    onClicked: stationListPopup.close()
                }
            }
            
            // 分隔线
            Rectangle {
                width: parent.width
                height: 2
                color: "#3498db"
                opacity: 0.3
            }
            
            // 基站列表
            ScrollView {
                width: parent.width
                height: parent.height - 100
                
                ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
                
                ListView {
                    model: baseStationModel
                    spacing: 10
                    
                    delegate: Rectangle {
                        width: stationListPopup.width - 80
                        height: 80
                        color: "#f8f9fa"
                        radius: 8
                        border.color: model.status === "normal" ? "#e0e0e0" : 
                                     model.status === "warning" ? "#f39c12" : "#e74c3c"
                        border.width: model.status === "normal" ? 1 : 2
                        
                        RowLayout {
                            anchors.fill: parent
                            anchors.margins: 15
                            spacing: 15
                            
                            // 图标区域
                            Rectangle {
                                width: 50
                                height: 50
                                color: model.status === "normal" ? "#27ae60" : 
                                      model.status === "warning" ? "#f39c12" : "#e74c3c"
                                radius: 25
                                
                                Text {
                                    anchors.centerIn: parent
                                    text: "📡"
                                    font.pixelSize: 24
                                }
                            }
                            
                            // 基站信息
                            Column {
                                Layout.fillWidth: true
                                spacing: 5
                                
                                Text {
                                    text: model.stationId + " - " + model.stationName
                                    font.pixelSize: 16
                                    font.bold: true
                                    color: "#2c3e50"
                                }
                                
                                Text {
                                    text: "位置: " + model.latitude.toFixed(2) + "°N, " + model.longitude.toFixed(2) + "°E"
                                    font.pixelSize: 13
                                    color: "#7f8c8d"
                                }
                            }
                            
                            // 状态信息
                            Column {
                                spacing: 3
                                
                                Text {
                                    text: model.status === "normal" ? "正常" : 
                                         model.status === "warning" ? "警告" : "严重"
                                    font.pixelSize: 14
                                    font.bold: true
                                    color: model.status === "normal" ? "#27ae60" : 
                                          model.status === "warning" ? "#f39c12" : "#e74c3c"
                                }
                                
                                Text {
                                    text: model.signalStrength.toFixed(1) + "%"
                                    font.pixelSize: 12
                                    color: "#7f8c8d"
                                }
                            }
                        }
                    }
                }
            }
        }
        
        // 进入动画
        enter: Transition {
            NumberAnimation {
                property: "opacity"
                from: 0
                to: 1
                duration: 200
            }
            NumberAnimation {
                property: "scale"
                from: 0.9
                to: 1
                duration: 200
            }
        }
        
        // 退出动画
        exit: Transition {
            NumberAnimation {
                property: "opacity"
                from: 1
                to: 0
                duration: 150
            }
            NumberAnimation {
                property: "scale"
                from: 1
                to: 0.9
                duration: 150
            }
        }
    }
    
    // 全屏告警历史弹窗
    Popup {
        id: alarmHistoryFullScreenPopup
        x: 0
        y: 0
        width: root.width
        height: root.height
        modal: true
        focus: true
        closePolicy: Popup.CloseOnEscape
        
        background: Rectangle {
            color: "#ecf0f1"
            radius: 0
            
            Rectangle {
                anchors.fill: parent
                anchors.margins: 30
                color: "white"
                radius: 15
                border.color: "#3498db"
                border.width: 2
                
                ColumnLayout {
                    anchors.fill: parent
                    anchors.margins: 25
                    spacing: 20
                    
                    // 标题栏
                    RowLayout {
                        Layout.fillWidth: true
                        
                        Text {
                            text: "🚨 告警历史详情"
                            font.pixelSize: 28
                            font.bold: true
                            color: "#2c3e50"
                        }
                        
                        Item {
                            Layout.fillWidth: true
                        }
                        
                        Text {
                            text: "共 " + alarmModel.rowCount() + " 条告警"
                            font.pixelSize: 16
                            color: "#7f8c8d"
                        }
                        
                        Item {
                            width: 20
                        }
                        
                        Button {
                            width: 100
                            height: 40
                            text: "清空"
                            onClicked: alarmModel.clearAll()
                            background: Rectangle {
                                color: parent.pressed ? "#c0392b" : parent.hovered ? "#e74c3c" : "#95a5a6"
                                radius: 8
                            }
                            contentItem: Text {
                                text: parent.text
                                color: "white"
                                font.pixelSize: 14
                                font.bold: true
                                horizontalAlignment: Text.AlignHCenter
                                verticalAlignment: Text.AlignVCenter
                            }
                        }
                        
                        Item {
                            width: 15
                        }
                        
                        Button {
                            width: 100
                            height: 40
                            text: "关闭"
                            background: Rectangle {
                                color: parent.pressed ? "#7f8c8d" : parent.hovered ? "#95a5a6" : "#bdc3c7"
                                radius: 8
                            }
                            contentItem: Text {
                                text: parent.text
                                color: "white"
                                font.pixelSize: 14
                                font.bold: true
                                horizontalAlignment: Text.AlignHCenter
                                verticalAlignment: Text.AlignVCenter
                            }
                            onClicked: alarmHistoryFullScreenPopup.close()
                        }
                    }
                    
                    // 分隔线
                    Rectangle {
                        Layout.fillWidth: true
                        height: 2
                        color: "#3498db"
                        opacity: 0.3
                    }
                    
                    // 告警列表
                    ListView {
                        Layout.fillWidth: true
                        Layout.fillHeight: true
                        model: alarmModel
                        clip: true
                        spacing: 12
                        
                        delegate: Rectangle {
                            width: ListView.view.width
                            height: 85
                            color: model.acknowledged ? "#ecf0f1" : "#fff3cd"
                            radius: 8
                            border.color: model.severity === "critical" ? "#e74c3c" : "#f39c12"
                            border.width: 2
                            
                            RowLayout {
                                anchors.fill: parent
                                anchors.margins: 15
                                spacing: 15
                                
                                // 级别图标
                                Rectangle {
                                    width: 60
                                    height: 60
                                    color: model.severity === "critical" ? "#e74c3c" : "#f39c12"
                                    radius: 30
                                    
                                    Text {
                                        anchors.centerIn: parent
                                        text: model.severity === "critical" ? "🚨" : "⚠️"
                                        font.pixelSize: 28
                                    }
                                }
                                
                                // 告警信息
                                ColumnLayout {
                                    Layout.fillWidth: true
                                    spacing: 8
                                    
                                    RowLayout {
                                        Layout.fillWidth: true
                                        
                                        Text {
                                            text: model.stationId + " - " + model.stationName
                                            font.pixelSize: 16
                                            font.bold: true
                                            color: model.severity === "critical" ? "#e74c3c" : "#f39c12"
                                        }
                                        
                                        Text {
                                            text: model.timestamp
                                            font.pixelSize: 13
                                            color: "#7f8c8d"
                                        }
                                        
                                        Item {
                                            Layout.fillWidth: true
                                        }
                                        
                                        Text {
                                            text: model.acknowledged ? "✓ 已确认" : "⚠ 未确认"
                                            font.pixelSize: 13
                                            font.bold: true
                                            color: model.acknowledged ? "#27ae60" : "#e67e22"
                                        }
                                    }
                                    
                                    Text {
                                        Layout.fillWidth: true
                                        text: model.message
                                        font.pixelSize: 14
                                        color: "#2c3e50"
                                        wrapMode: Text.WordWrap
                                    }
                                }
                                
                                // 状态徽章
                                Rectangle {
                                    width: 80
                                    height: 35
                                    color: model.severity === "critical" ? "#e74c3c" : "#f39c12"
                                    radius: 17
                                    
                                    Text {
                                        anchors.centerIn: parent
                                        text: model.severity === "critical" ? "严重" : "警告"
                                        font.pixelSize: 13
                                        font.bold: true
                                        color: "white"
                                    }
                                }
                            }
                            
                            MouseArea {
                                anchors.fill: parent
                                onClicked: {
                                    // 单击：高亮显示对应基站在地图上
                                    mapView.highlightStation(model.stationId)
                                    
                                    // 确认告警
                                    if (!model.acknowledged) {
                                        alarmModel.acknowledgeAlarm(index)
                                    }
                                }
                                
                                onDoubleClicked: {
                                    // 双击：显示报告对话框（传递完整的告警信息）
                                    var alarmInfo = alarmModel.getAlarmInfo(index)
                                    alarmReportDialog.showReportWithInfo(alarmInfo)
                                }
                            }
                        }
                        
                        ScrollBar.vertical: ScrollBar {
                            policy: ScrollBar.AsNeeded
                        }
                    }
                }
            }
        }
        
        // 进入动画
        enter: Transition {
            NumberAnimation {
                property: "opacity"
                from: 0
                to: 1
                duration: 250
            }
        }
        
        // 退出动画
        exit: Transition {
            NumberAnimation {
                property: "opacity"
                from: 1
                to: 0
                duration: 200
            }
        }
    }
}

